package com.hyz.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.hyz.pojo.AssetImport;
import com.hyz.service.AssetImportService;
import com.hyz.service.AssetManagerService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;

/**
 * @author HGD
 * @date 2022/10/3 22:25
 */
@Component
public class AssetManagerListener {
    @Autowired
    private AssetImportService assetImportService;
    @Autowired
    private AssetManagerService assetManagerService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "assetImport.queue"),
            exchange = @Exchange(name = "assetManager", type = ExchangeTypes.DIRECT),
            key = {"insertAssetImport"}
    ))
    public void insertAssetImport(String massage) {
        ArrayList<AssetImport> assetImports = JSON.parseObject(massage, new TypeReference<ArrayList<AssetImport>>() {
        });
        assetImportService.saveBatch(assetImports);
        assetManagerService.batchInsertAssetLogByAssetImports(assetImports);
    }
}
